import 'package:flutter_rtmp/flutter_rtmp.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shop/common/shop_material.dart';

class RtmpPage extends StatefulWidget {
  RtmpPage({Key key}) : super(key: key);
  @override
  _RtmpPageState createState() => _RtmpPageState();
}

class _RtmpPageState extends State<RtmpPage> {
  RtmpManager _manager;
  bool show = false;
  @override
  void initState() {
    super.initState();
    _getPermission();
    _manager = RtmpManager();
  }

  _getPermission() async {
    await [
      Permission.camera,
      Permission.locationAlways,
    ].request();
    if (await Permission.camera.isGranted) {
      setState(() {
        show = true;
      });
    } else {
      openAppSettings();
    }
  }

  @override
  void dispose() {
    _manager.stopLive();
    _manager.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.black,
        child: Stack(
          children: <Widget>[
            AnimatedContainer(
              duration: Duration(seconds: 1),
              child: show
                  ? RtmpView(manager: _manager)
                  : Container(
                      color: Colors.black,
                    ),
            ),
            Positioned(
                top: 200,
                child: FlatButton(
                    onPressed: () {
                      _manager.startLive("rtmp://192.168.124.48:1935/live/yyy");
                    },
                    child: Text("开始"))),
            Positioned(
                top: 300,
                child: FlatButton(
                    onPressed: () {
                      _manager.stopLive();
                    },
                    child: Text("结束"))),
          ],
        ),
      ),
    );
  }
}
